
/* Generated by Interface Builder */

#import "MyObject.h"

@implementation MyObject

+ new
{
  self = [super new];
  openReq = [OpenPanel new];
  return self;
}

- showError: (char *)errorMessage
{
  NXRunAlertPanel("Error", errorMessage,"OK",NULL,NULL);
}

// openRequest: opens a new file. It puts up a open panel, and, if the user
// doesn't cancel, it reads the specified archive file. If the selected file
// is not a proper archive file, then openRequest: will complain.

- openRequest:sender
{
    const char *fileName;
    const char *const types[4] = {"data",NULL};
    int ok;

    if ([openReq runModalForTypes:types] && (fileName =[openReq filename])) {
	[self openFile:fileName];
    }
    else
	[self showError:"No file chosen or could not open file"];
    return self;
}

-(int) openFile:(const char *)fileName
{
  int i=0;
  char line[MAX_CHARS_PER_LINE];
  char string[MAX_CHARS_PER_LINE][MAX_LINES];
  int value[MAX_LINES];
  FILE *input_fp;
  printf("Opening File = %s\n", fileName);
  
  if (( input_fp = fopen(fileName, "r") ) == NULL ) {
     [self showError:"Could not open file"];
     fprintf(stderr, "File: %s Could not be opened.\n", fileName);
     return NO;
  }
  
  while (fgets(line, MAX_CHARS_PER_LINE, input_fp) != NULL)
  {
     i++;
     if (i >= MAX_LINES) break;
     sscanf(line, "%d %s", &value[i], string[i]);
     printf("  value = %d label = %s\n", value[i], string[i]);
  }
  printf("%d data points read\n", i);
  fclose(input_fp);
  return YES;

}

